package com.example.goodpet.home.homeRecommend.adapter.viewholder;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;

import com.example.goodpet.R;
import com.example.goodpet.activity.MainActivity;
import com.example.goodpet.activity.MainChannelActivity;
import com.example.goodpet.activity.PetFoodActivity;
import com.example.goodpet.activity.PetHospitalActivity;
import com.example.goodpet.activity.PetTipsActivity;
import com.example.goodpet.activity.PetUtensilsActivity;
import com.example.goodpet.bean.ChannelInfoDTO;
import com.example.goodpet.bean.ResponseBody;
import com.example.goodpet.home.homeRecommend.adapter.adapters.ChannelAdapter;
import com.example.goodpet.home.homeRecommend.bean.ResultBeanData;
import com.example.goodpet.home.homeRecommend.bean.TaobaoURL;
import com.example.goodpet.service.HomeService;
import com.example.goodpet.utils.Constants;
import com.example.goodpet.utils.DeviceUtil;
import com.example.goodpet.utils.RetrofitSingleton;
import com.example.goodpet.utils.UIUtils;

import java.io.IOException;
import java.util.List;

import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ChannelViewHolder extends RecyclerView.ViewHolder {

    private Context mContext;
    private GridView gv_channel;
    private ChannelAdapter adapter;
    private String urlY;
    private String urlN;
    private String TAG=this.getClass().getSimpleName();

    public ChannelViewHolder(Context mContext, View itemView) {
        super(itemView);
        this.mContext=mContext;
        gv_channel= itemView.findViewById(R.id.gv_channel_main);
        //设置item的点击事件
        gv_channel.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                if(position==0){
                    Intent intent=new Intent(mContext, PetHospitalActivity.class);
                    mContext.startActivity(intent);
                }else if(position==1){
                    Intent intent=new Intent(mContext, PetUtensilsActivity.class);
                    mContext.startActivity(intent);
                }else if(position==2){
                    getTabaoUrl();
                }else if(position==3){
                    Intent intent=new Intent(mContext, PetTipsActivity.class);
                    mContext.startActivity(intent);
                }
            }
        });
    }

    private void getTabaoUrl() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constants.JSON_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        retrofit.create(HomeService.class).getTaobaoJson()
                .enqueue(new Callback<TaobaoURL>() {
                    @Override
                    public void onResponse(Call<TaobaoURL> call, Response<TaobaoURL> response) {
                        TaobaoURL body = response.body();
                        if(body.getCode()==200){
                            urlY=body.getResult().getUrlY();
                            urlN=body.getResult().getUrlN();
                            openTaobaoShopping();
                        }else{
                            UIUtils.toast("获取淘宝链接失败",false);
                        }
                    }

                    @Override
                    public void onFailure(Call<TaobaoURL> call, Throwable t) {

                    }
                });

    }

    private void openTaobaoShopping() {
        if (DeviceUtil.isInstall(mContext, "com.taobao.taobao")) {
            //安装了淘宝
            Intent intent = new Intent();
            intent.setAction("android.intent.action.VIEW");
            Uri uri = Uri.parse(urlY);
            intent.setData(uri);
            mContext.startActivity(intent);
        } else {
            //没安装淘宝，打开网页版淘宝
            open(mContext, urlN);
        }
    }

    private void open(Context mContext, String url) {
        Intent intent = new Intent();
        intent.setClass(mContext,PetFoodActivity.class);
        intent.putExtra("webUrl",url);
        mContext.startActivity(intent);
    }

    public void setData(List<ChannelInfoDTO> channelInfo) {
        adapter=new ChannelAdapter(mContext,channelInfo);
        gv_channel.setAdapter(adapter);
    }
}
